/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.sanerzone.smscenter.modules.sms.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sanerzone.jeebase.common.utils.JedisUtils;
import com.sanerzone.smscenter.modules.sms.entity.SmsReceive;
import com.sanerzone.smscenter.modules.sms.entity.WhiteListConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sanerzone.jeebase.common.config.Global;
import com.sanerzone.jeebase.common.persistence.Page;
import com.sanerzone.jeebase.common.web.BaseController;
import com.sanerzone.jeebase.common.utils.StringUtils;
import com.sanerzone.smscenter.modules.sms.entity.SmsPhoneWhitelist;
import com.sanerzone.smscenter.modules.sms.service.SmsPhoneWhitelistService;
import redis.clients.jedis.Jedis;

/**
 * 系统白名单Controller
 * @author zhukc
 * @version 2017-06-26
 */
@Controller
@RequestMapping(value = "${adminPath}/sms/smsPhoneWhitelist")
public class SmsPhoneWhitelistController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(SmsPhoneWhitelistController.class);

	@Autowired
	private SmsPhoneWhitelistService smsPhoneWhitelistService;
	
	@ModelAttribute
	public SmsPhoneWhitelist get(@RequestParam(required=false) String id) {
		SmsPhoneWhitelist entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = smsPhoneWhitelistService.get(id);
		}
		if (entity == null){
			entity = new SmsPhoneWhitelist();
		}
		return entity;
	}
	
	@RequiresPermissions("sms:smsPhoneWhitelist:view")
	@RequestMapping(value = {"list", ""})
	public String init(SmsPhoneWhitelist smsPhoneWhitelist, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SmsPhoneWhitelist> page = smsPhoneWhitelistService.findPage(new Page<SmsPhoneWhitelist>(request, response), smsPhoneWhitelist);
		model.addAttribute("page", page);
		return "modules/sms/smsPhoneWhitelistList";
	}

	@RequiresPermissions("sms:smsPhoneWhitelist:view")
	@RequestMapping(value = "form")
	public String form(SmsPhoneWhitelist smsPhoneWhitelist, Model model) {
		model.addAttribute("smsPhoneWhitelist", smsPhoneWhitelist);
		return "modules/sms/smsPhoneWhitelistForm";
	}

	@RequiresPermissions("sms:smsPhoneWhitelist:edit")
	@RequestMapping(value = "save")
	public String save(SmsPhoneWhitelist smsPhoneWhitelist, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, smsPhoneWhitelist)){
			return form(smsPhoneWhitelist, model);
		}
		smsPhoneWhitelistService.save(smsPhoneWhitelist);
		addMessage(redirectAttributes, "保存系统白名单成功");
		return "redirect:"+Global.getAdminPath()+"/sms/smsPhoneWhitelist/?repage";
	}
	
	@RequiresPermissions("sms:smsPhoneWhitelist:edit")
	@RequestMapping(value = "delete")
	public String delete(SmsPhoneWhitelist smsPhoneWhitelist, RedirectAttributes redirectAttributes) {
		smsPhoneWhitelistService.delete(smsPhoneWhitelist);
		addMessage(redirectAttributes, "删除系统白名单成功");
		return "redirect:"+Global.getAdminPath()+"/sms/smsPhoneWhitelist/?repage";
	}
	@RequiresPermissions("sms:smsPhoneWhitelist:edit")
	@RequestMapping(value = "saveConfig")
	public String saveConfig(WhiteListConfig config) {
		//校验只能是数字
		try {
			Integer.parseInt(  config.getMatchMinSizeStr());
			Integer.parseInt( config.getMatchMinArea() );
			Integer.parseInt( config.getEachAreaMinNum() );
			Integer.parseInt( config.getMaxRepeatNum() );
			Integer.parseInt( config.getMinContentSize() );
			Integer.parseInt( config.getMaxPhoneSizeByContent() );
		}catch (Exception e){
			return null;
		}
		Jedis jedis=null;
		boolean isSuc=true;
		try{
			jedis=JedisUtils.getResource();
			jedis.set( "matchMinSize",config.getMatchMinSizeStr() );
			jedis.set( "matchMinArea",config.getMatchMinArea() );
			jedis.set( "eachAreaMinNum",config.getEachAreaMinNum() );
			jedis.set( "maxRepeatNum",config.getMaxRepeatNum() );
			jedis.set( "minContentSize",config.getMinContentSize() );
			jedis.set( "maxPhoneSizeByContent",config.getMaxPhoneSizeByContent() );

		}catch (Exception e){
			logger.error( "获取jedis错误。" );
			isSuc=false;
		}finally {
			if (jedis!=null){
				jedis.close();
			}
		}

		return "redirect:"+Global.getAdminPath()+"/sms/smsPhoneWhitelist/?repage";
	}



	@RequiresPermissions("sms:smsPhoneWhitelist:view")
	@RequestMapping(value = "configForm")
	public String configForm(WhiteListConfig config,Model model){
		Jedis jedis=null;
		try{
			jedis=JedisUtils.getResource();
			String matchMinSizeStr=jedis.get( "matchMinSize" );
			String matchMinArea=jedis.get( "matchMinArea" );
			String eachAreaMinNum=jedis.get( "eachAreaMinNum" );
			String maxRepeatNum=jedis.get( "maxRepeatNum" );
			String minContentSize=jedis.get( "minContentSize" );
			String maxPhoneSizeByContent=jedis.get( "maxPhoneSizeByContent" );
			config.setMatchMinSizeStr( matchMinSizeStr );
			config.setMatchMinArea( matchMinArea );
			config.setEachAreaMinNum( eachAreaMinNum );
			config.setMaxRepeatNum( maxRepeatNum );
			config.setMinContentSize( minContentSize );
			config.setMaxPhoneSizeByContent( maxPhoneSizeByContent );
			model.addAttribute( "config",config );



		}catch (Exception e){
			logger.error( "jedis获取失败" );
		}finally {
			if (jedis!=null){
				jedis.close();
			}
		}

		return "modules/sms/whiteListConfig";
	}


}